<template>
	<view class="pay">
		<view class="pay_header bg-white">
			Payment method
		</view>
		<view class="pay_list">
			<ul>
				<li v-for="(item,index) in methodList" :key="index" @click.stop="onMethods(item,index)">
					<view class="pay_left row">
						<image :src="item.logo"></image>
						<text>{{locale=='en'?item.name_en:item.name_ar}}</text>
					</view>
					<image src="../../../static/select.png" mode="widthFix" v-if="methodsIndex==index"></image>
				</li>
			</ul>
		</view>
		<view class="pay_header bg-white">
			Currency
		</view>
		<view class="pay_list">
			<ul>
				<li v-for="(item,index) in currencyList" :key="index" @click.stop="handleCurrency(item,index)">
					<text>{{item}}</text>
					<image src="../../../static/select.png" mode="widthFix" v-if="currencyIndex==index"></image>
				</li>
			</ul>
		</view>
		<view class="reserve_item">
			<view class="item_title">{{ $t('full.name') }}</view>
			<view class="phoneInput">
				<input type="text" v-model="firstname" autocomplete="off" :always-embed="true" :adjust-position="true"
					cursor-spacing="240" placeholder="First name" />

			</view>
			<view class="phoneInput">
				<input type="text" v-model="lastname" autocomplete="off" :always-embed="true" :adjust-position="true"
					cursor-spacing="240" placeholder="Last name" />
			</view>

		</view>
		<view class="pay_price row between">
			<view class="pay_left row">
				<text>price</text>（{{currency}}）
			</view>
			<text>￥{{info.actual_money}}</text>
		</view>
		<view class="pay_btn">
			<button @click.stop="handlePay">PAY NOW</button>
		</view>
	</view>
</template>

<script>
	import {
		createReserveOrder
	} from "@/lib/service"
	export default {
		data() {
			return {
				// firstname: "Fawaterak",
				// lastname: "test",
				firstname: "",
				lastname: "",
				methodList: [],
				methodsIndex: 0,
				methodInfo: null,
				currencyList: ['EGP','USD', 'SAR', 'AED', 'KWD', 'QAR', 'BHD'],
				currency: "EGP",
				currencyIndex: 0,
				info: null
			}
		},

		computed: {
			locale() {
				return this.$store.state.language;
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.info));
			this.info = data;
			this.handleMethods();
		},
		methods: {
			handleMethods() {
				let _this = this;
				uni.request({
					url: 'https://app.fawaterk.com/api/v2/getPaymentmethods',
					method: 'GET',
					header: {
						'Content-Type': 'application/json',
						'Authorization': 'Bearer 899fa314b849de3a21b68fd4b3993a443f93fc905b537107db'
					},
					data: {
						method: 'GET',
						redirect: 'follow'
					},
					success: (res) => {
						_this.methodList = res.data.data;
						_this.methodInfo = _this.methodList[0];
					},
					fail: (err) => {
						console.error('API调用失败:', err);
						uni.showToast({
							title: '网络错误，请重试',
							icon: 'none'
						});
					}
				});
			},
			onMethods(item, index) {
				this.methodsIndex = index;
				this.methodInfo = item;
			},
			handleCurrency(item, index) {
				this.currencyIndex = index;
				this.currency = item;
			},
			handlePay() {
				let _this = this;
				if (!this.firstname) {
					uni.showToast({
						title: 'Please enter your first name',
						icon: 'none'
					})
					false;
				}
				if (!this.lastname) {
					uni.showToast({
						title: 'Please enter your last name',
						icon: 'none'
					})
					false;
				}
				let obj = {
					settleno: this.info.settleno,
					persons: this.info.persons,
					name: this.info.name,
					phone: this.info.phone,
					note: this.info.note,
					method: this.locale == 'en' ? this.methodInfo.name_en : this.methodInfo.name_ar,
					currency: this.currency,
					firstname: this.firstname,
					lastname: this.lastname,
				}
				createReserveOrder(obj).then((res) => {
					if (res.statusCode == 0) {
						let requestData = {
							payment_method_id: _this.methodInfo.paymentId,
							cartTotal: _this.info.actual_money,
							currency: _this.currency,
							redirectOption:true,
							"redirectionUrls": {
								"successUrl": "https://www.anta123.com/pages/index/reserve/success",
								"failUrl": "https://www.anta123.com/pages/index/reserve/fail",
								"pendingUrl": "https://www.anta123.com/pages/index/reserve/pending?orderno="+res.data.orderno
							},
							"customer": {
								first_name: _this.firstname,
								last_name: _this.lastname,
							},
							"payLoad": {
								orderno: res.data.orderno,
							},
							"cartItems": [{
								name: "test",
								price: _this.info.actual_money,
								quantity: "1"
							}]
						};
						if(this.methodInfo.paymentId==4){
							requestData.customer['phone'] = _this.info.phone;
						}
						uni.request({
							url: 'https://app.fawaterk.com/api/v2/invoiceInitPay',
							method: 'POST',
							header: {
								'Content-Type': 'application/json',
								'Authorization': 'Bearer 899fa314b849de3a21b68fd4b3993a443f93fc905b537107db'
							},
							data: requestData,
							success: (res) => {
								if (res.data.data.payment_data.redirectTo) {
									uni.navigateTo({
										url: `./go?url=${res.data.data.payment_data.redirectTo}`
									});
								} else {
									uni.showToast({
										title: `错误: ${res.data.data.payment_data.error}`,
										icon: 'none'
									});
								}
							},
							fail: (err) => {
								console.error('API调用失败:', err);
								uni.showToast({
									title: '网络错误，请重试',
									icon: 'none'
								});
							}
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}

				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pay {
		width: 100%;

		.pay_header {
			padding: 32rpx 36rpx;
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			font-size: 42rpx;
			color: #333333;
		}

		.pay_list {
			padding: 0 36rpx;
			background: #F9F9F9;

			ul {
				li {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 30rpx 0;
					border-bottom: 1px solid #F1F1F1;

					.pay_left {
						>image {
							width: 80rpx;
							height: 32rpx;
							margin-right: 30rpx;
						}

						text {
							font-family: Noto Sans S Chinese;
							font-weight: 400;
							font-size: 28rpx;
							color: #1E1E1E;
						}
					}

					>image {
						width: 34rpx;
					}
				}
			}
		}

		.reserve_item {
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			font-size: 26rpx;
			position: relative;
			padding: 40rpx 32rpx;

			.phoneInput {
				width: 682rpx;
				height: 88rpx;
				background: #F3F3F3;
				border-radius: 10rpx;
				margin-top: 24rpx;

				input {
					width: 90%;
					height: 88rpx;
					padding-left: 30rpx;
					font-family: Microsoft YaHei UI;
					font-weight: 400;
					font-size: 30rpx;
					color: #333333;
				}
			}

			.item_title {
				font-family: Microsoft YaHei UI;
				font-weight: 400;
				font-size: 42rpx;
				color: #333333;
			}
		}

		.pay_price {
			padding: 0 32rpx;
		}

		.pay_btn {
			padding: 40rpx 0;
			button {
				width: 630rpx;
				height: 90rpx;
				background: #4980F7;
				border-radius: 45rpx;
				font-weight: 400;
				font-size: 32rpx;
				color: #FEFEFE;
				line-height: 90rpx;
			}
		}
	}
</style>